<template>
    <div class="register">
        注册页面
        <!-- 步骤条 -->
        <van-steps :active="active">
            <van-step>验证手机号</van-step>
            <van-step>发送验证码</van-step>
            <van-step>注册账号</van-step>
            <van-step>完成注册</van-step>
        </van-steps>

        <!-- 步骤 -->
        <div v-if="active === 0">
            <van-cell-group inset>
                <van-field v-model="tel" label="手机号" placeholder="输入用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-button type="primary" @click="handleVerifyPhone">验证</van-button>
            </van-cell-group>
        </div>

        <div v-if="active === 1">
            <van-cell-group inset>
                <van-field v-model="telcode" center clearable label="短信验证码" placeholder="请输入短信验证码">
                    <template #button>
                        <van-button size="small" type="primary" @click="handleSendCode">发送验证码</van-button>
                    </template>
                </van-field>
                <van-button type="primary" @click="handleVerifyCode">下一步</van-button>
            </van-cell-group>
        </div>

        <div v-if="active === 2">
            <van-cell-group>
                <van-field v-model="tel" label="手机号" placeholder="请输入手机号" />
                <van-field v-model="password" type="password" label="密码" placeholder="请输入密码" />
                <van-button type="primary" @click="handleRegister">注册</van-button>
            </van-cell-group>
        </div>

        <div v-if="active === 3">
            <span>注册成功</span>
            <router-link to="/login">去登录</router-link>
        </div>

    </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { showToast } from 'vant';
import { checkPhone, sendCode, verifyCode, register } from '@/api/login-register'

// 步骤进度条索引
const active = ref(0);

// 手机号
const tel = ref<string>('17586688668')

// 验证码
const telcode = ref<string>('')

// 密码
const password = ref<string>('666666')

/** 
 * 第一步：验证手机号是否可注册
 */
const handleVerifyPhone = async () => {
    console.log('验证手机号...');
    // 获取输入的手机号
    if (!tel.value.trim()) {
        showToast({
            type: 'fail',
            message: '请输入手机号',
        })
        return
    }
    try {
        const result = await checkPhone(tel.value)
        console.log('第一步:', result);
        active.value = 1
    } catch (error: any) {
        console.log('手机号验证异常：', error);
        showToast({
            type: 'fail',
            message: error.message,
        })
    }

}

/**
 * 第二步：1-发送验证码
 */
const handleSendCode = async () => {
    try {
        const result: any = await sendCode(tel.value)
        console.log('第二步1-：', result);
        telcode.value = result.code
    } catch (error: any) {
        showToast({
            type: 'fail',
            message: error.message,
        })
    }
}

/**
 * 第二步：2-验证手机验证码是否输入正确
 */
const handleVerifyCode = async () => {
    try {
        const result = await verifyCode(tel.value, telcode.value)
        console.log('第二步2-：', result);
        active.value = 2
    } catch (error: any) {
        showToast({
            type: 'fail',
            message: error.message,
        })
    }
}

/**
 * 第三步：注册
 */
const handleRegister = async () => {
    if (!password.value.trim()) {
        showToast({
            type: 'fail',
            message: '请输入密码',
        })
        return
    }
    try {
        const result = await register(tel.value, password.value)
        console.log('第三步:', result);
        active.value = 3
    } catch (error: any) {
        showToast({
            type: 'fail',
            message: error.message,
        })
    }
}
</script>

<style lang="less" scoped></style>
